<template>
  <div class="style-page">
    <div class="head-page-title">{{ showComponentData.title }}</div>
    <div class="line"></div>

    <div class="component-style">
      <div class="header">
        <p class="title">组件样式</p>
      </div>
      <el-form-item label="公告图标：">
        <div class="avatar-uploader" @click="showImageChooseDialog = true">
          <el-image
            v-if="showComponentData.notice_icon"
            :src="showComponentData.notice_icon"
            class="avatar"
            fit="contain"
          ></el-image>
          <i v-else class="el-icon-plus avatar-uploader-icon"
            ><el-icon> <Plus /> </el-icon
          ></i>
          <div v-if="showComponentData.notice_icon" class="mask">替换</div>
          <el-icon
            v-if="showComponentData.notice_icon"
            class="del-image"
            :size="20"
            @click.stop="handleDelImg"
            ><CircleCloseFilled
          /></el-icon>
        </div>
      </el-form-item>
      <!--      <el-form-item label="是否滚动：">-->
      <!--        <el-switch v-model="showComponentData.scroll" />-->
      <!--      </el-form-item>-->
      <el-form-item label="动画效果">
        <el-radio-group v-model="showComponentData.direction">
          <el-radio value="vertical">翻页滚动</el-radio>
          <el-radio value="horizontal">横向滚动</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="字体大小：">
        <div class="flex">
          <el-slider style="width: 70%" v-model="form.fontSize" :min="12" :max="30" />
          <el-input
            style="width: 30%; margin-left: 20px"
            v-model="form.fontSize"
            :min="12"
            :max="30"
          >
            <template #append>px</template>
          </el-input>
        </div>
      </el-form-item>
      <el-form-item label="文字颜色：">
        <el-color-picker v-model="form.color"></el-color-picker>
        <el-input style="width: 160px; margin-left: 10px" v-model="form.color">
          <template #append>
            <el-button @click="form.color = '#333333'">重置</el-button>
          </template>
        </el-input>
      </el-form-item>
      <!--      <el-form-item label="背景颜色：">-->
      <!--        <el-color-picker v-model="form.backgroundColor"></el-color-picker>-->
      <!--        <el-input style="width: 160px; margin-left: 10px" v-model="form.backgroundColor">-->
      <!--          <template #append>-->
      <!--            <el-button @click="form.backgroundColor = '#fff'">重置</el-button>-->
      <!--          </template>-->
      <!--        </el-input>-->
      <!--      </el-form-item>-->

      <!--      <el-form-item label="圆角：">-->
      <!--        <div class="flex">-->
      <!--          <el-slider style="width: 70%" v-model="form.borderRadius" />-->
      <!--          <el-input style="width: 30%; margin-left: 20px" v-model="form.borderRadius">-->
      <!--            <template #append>px</template>-->
      <!--          </el-input>-->
      <!--        </div>-->
      <!--      </el-form-item>-->
    </div>

    <div class="component-style">
      <div class="header">
        <p class="title">内容样式</p>
      </div>

      <VueDraggable
        v-model="showComponentData.default_sub_list"
        :animation="150"
        ghostClass="ghost"
        :filter="'.el-input'"
        :preventOnFilter="false"
        class="content"
        @end="handleUpdateComponent"
      >
        <div
          v-for="(item, index) in showComponentData.default_sub_list"
          :key="item.id"
          class="notice-item"
        >
          <div class="operation">
            <div class="column">
              <p class="title">公告内容：</p>
              <el-input v-model="item.title"></el-input>
            </div>
            <div class="column">
              <p class="title">跳转链接：</p>
              <div class="link">
                <el-icon style="color: var(--el-color-primary)"><Link /></el-icon>
                <el-link type="primary" @click="handleOpenLinkDialog(index)">{{
                  item.link?.name || '请选择跳转链接'
                }}</el-link></div
              >
            </div>
          </div>

          <el-icon class="del-icon" :size="20" @click="handleDel(index)"
            ><CircleCloseFilled
          /></el-icon>
        </div>

        <div>
          <!--          <el-button type="primary" @click="handleAdd" plain :icon="Plus" style="width: 100%"-->
          <!--            >添加公告({{ showComponentData.default_sub_list.length }}/{{-->
          <!--              showComponentData.max_list_count-->
          <!--            }})</el-button-->
          <!--          >-->
          <el-tag type="primary" class="submit-btn" @click="handleAdd"
            >添加公告 ({{ showComponentData.default_sub_list.length }}/{{
              showComponentData.max_list_count
            }})</el-tag
          >
        </div>
      </VueDraggable>
    </div>

    <div class="component-style">
      <common-style
        :content-data="showComponentData"
        @update-style="handleUpdateStyle"
      ></common-style>
    </div>

    <link-list-dialog
      v-if="showLinkDataDialog"
      :show-link-dialog="showLinkDataDialog"
      @close-dialog="handleCloseLinkDialog"
    ></link-list-dialog>

    <image-selected-dialog
      v-if="showImageChooseDialog"
      :single-type="true"
      :operation-type="false"
      :show-image-choose-dialog="showImageChooseDialog"
      @close-dialog="handleCloseImageDialog"
    ></image-selected-dialog>
  </div>
</template>

<script setup lang="ts">
  import debounce from 'lodash/debounce'
  import CommonStyle from '@/views/decoration/diyComponents/CommonStyleComponent/CommonStyle.vue'
  import { CircleCloseFilled, Link, Plus } from '@element-plus/icons-vue'
  import { ElMessage } from 'element-plus'
  import { VueDraggable } from 'vue-draggable-plus'
  import LinkListDialog from '@/views/decoration/diyComponents/LinkListDialog.vue'
  import ImageSelectedDialog from '@/views/decoration/diyComponents/ImageSelectedDialog.vue'

  const props = defineProps({
    contentData: {
      type: Object,
      default: () => ({})
    }
  })
  const emit = defineEmits(['updateData'])

  const showComponentData = ref(props.contentData)
  const showLinkDataDialog = ref(false)
  const changeIndex = ref()
  const showImageChooseDialog = ref(false)

  const form = reactive({
    fontSize: 14,
    color: '#333333'
    // backgroundColor: '#fff',
    // borderRadius: 0
  })

  watch(
    form,
    () => {
      emitFormUpdate()
    },
    { deep: true }
  )

  const emitFormUpdate = debounce(() => {
    showComponentData.value.itemStyle = {
      fontSize: form.fontSize,
      color: form.color
      // borderRadius: form.borderRadius,
      // backgroundColor: form.backgroundColor
    }
    handleUpdateComponent()
  }, 300)

  function handleUpdateStyle(val: any) {
    showComponentData.value.style = val
    handleUpdateComponent()
  }

  function handleUpdateComponent() {
    emit('updateData', showComponentData.value)
  }

  // const handleSuccess = (file: any) => {
  //   showComponentData.value.notice_icon = file.data.url
  //   handleUpdateComponent()
  // }

  function handleDel(index: number) {
    showComponentData.value.default_sub_list.splice(index, 1)
    handleUpdateComponent()
  }

  function handleAdd() {
    if (showComponentData.value.default_sub_list.length > showComponentData.value.max_list_count) {
      return
    }

    showComponentData.value.default_sub_list.push({
      id: new Date().getTime(),
      title: '新公告'
    })
  }

  function handleOpenLinkDialog(index: number) {
    showLinkDataDialog.value = true
    changeIndex.value = index
  }

  function handleCloseLinkDialog(value: any) {
    if (value) {
      showComponentData.value.default_sub_list[changeIndex.value].link = value
    }
    showLinkDataDialog.value = false
  }

  function handleDelImg() {
    showComponentData.value.notice_icon = ''
    handleUpdateComponent()
  }

  function handleCloseImageDialog(value: any) {
    if (value) {
      if (Array.isArray(value)) {
        showComponentData.value.default_sub_list.push(...value)
      } else {
        showComponentData.value.notice_icon = value.fileUrl
      }
      handleUpdateComponent()
    }
    showImageChooseDialog.value = false
  }

  onMounted(() => {
    if (showComponentData.value.itemStyle.fontSize) {
      form.fontSize = showComponentData.value.itemStyle.fontSize
      form.color = showComponentData.value.itemStyle.color
    }
  })
</script>

<style scoped lang="scss">
  .style-page {
    padding: 20px;

    .head-page-title {
      padding-top: 10px;
      padding-bottom: 20px;
      font-size: 18px;
      font-weight: bold;
      color: black;
    }

    .head-title {
      padding: 20px 0;
      font-size: 14px;
      font-weight: bold;
      color: black;
    }

    .line {
      width: 100%;
      height: 1px;
      background: #f5f7fa;
    }

    .submit-btn {
      width: 100%;
      height: 40px !important;
      cursor: pointer;
      font-size: 14px;
    }

    .component-style {
      margin: 20px 0;

      .header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 10px;

        .title {
          font-size: 14px;
          font-weight: bold;
          color: black;
        }
      }

      .avatar-uploader {
        border: 1px dashed #d9d9d9;
        border-radius: 12px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        width: 48px;
        height: 48px;
        background-color: #fff;

        .mask {
          position: absolute;
          bottom: 0;
          left: 0;
          width: 100%;
          height: 24px;
          background-color: rgba(0, 0, 0, 0.6);
          display: flex;
          align-items: center;
          justify-content: center;
          opacity: 0;
          transition: opacity 0.3s ease;
          color: #fff;
          font-size: 12px;
        }

        .del-image {
          display: none;
          position: absolute;
          top: 0;
          right: 0;
          cursor: pointer;
          color: #8c939d;
        }

        &:hover .mask {
          opacity: 1;
        }

        &:hover .del-image {
          display: block;
        }
      }

      .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 48px;
        height: 48px;
        line-height: 48px;
        text-align: center;
      }

      .avatar {
        width: 100%;
        height: 48px;
        display: block;
      }

      .flex {
        display: flex;
        justify-content: space-between;
        width: 100%;
        padding: 0 3px;
      }
    }

    .content {
      .notice-item {
        width: 100%;
        margin-bottom: 20px;
        display: flex;
        align-items: center;
        position: relative;
        background-color: #f7f8fa;
        padding: 10px;
        box-sizing: border-box;

        &:hover {
          .del-icon {
            display: block;
          }
        }

        .operation {
          flex: 1;

          .column {
            display: flex;
            align-items: center;
            margin-bottom: 6px;
            width: 100%;

            .title {
              width: 100px;
              font-size: 14px;
              margin-right: 20px;
            }

            .link {
              width: 100%;
              display: flex;
              align-items: center;
              height: 40px;
              border-radius: 6px;
              background-color: #fff;
              padding-left: 10px;
            }
          }
        }

        .del-icon {
          display: none;
          position: absolute;
          top: -2px;
          right: -2px;
          cursor: pointer;
          color: #8c939d;
        }
      }
    }
  }
</style>
